<?php
header("Content-Type: text/html; charset=iso-8859-1");
class partenaire{

	var $fields;

	function partenaire($value = '',$field='id'){
		$fields = describe('partenaires');
		$q = "select * from partenaires where $field = '$value'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
			if(strpos($this->url,"www") == 0) $this->url = "http://".$this->url;
		}
		else{
			foreach($fields as $field => $type){
				$this->$field = "";
			}
		}
		if(!$this->image) $this->image = $this->id.".jpeg";
	}

	function insert(){
		$fields = describe('partenaires');
		$q = "insert into partenaires (";
		$q1 = ") values(";
		foreach($fields as $field => $type){
			if($field != 'image'){
				$q .= $field.", ";
				$q1 .= "\"".(addslashes($this->$field))."\", ";
			}
		}
		$q = substr($q,0,-2);
		$q1 = substr($q1,0,-2);
		$q .= $q1.")";
		if(ExecRequete($q)) return mysql_insert_id();
		else  return false;
	}

	function update(){
		$fields = describe('partenaires');
		if($this->id){
			$q = "update partenaires set ";
			foreach($fields as $field => $type)	{
				$q .= "$field = \"".(addslashes($this->$field))."\", ";
			}
			$q = substr($q,0,-2);
			$q .= " where id = '$this->id'";
			if(ExecRequete($q)) return true;
			else return false;
		}
		else $this->insert();
	}

	function delete(){
		$q = "delete from partenaires where id = '$this->id'";
		if(ExecRequete($q)){
			if(file_exists("partenaires/$this->image")) unlink("partenaires/$this->image");
			return true;
		}
		else return false;
	}

	function logo($width = 180,$shadow=1){
		$image = dirname(dirname(__FILE__))."/images/partenaires/$this->image";
		if(!file_exists($image) && file_exists(str_replace(".jpg",".jpeg",$image))) $image = str_replace(".jpg",".jpeg",$image);
		if(file_exists($image) && $this->image){
			$size =getimagesize(dirname(dirname(__FILE__))."/images/partenaires/$this->image");
			if($size[0] > $width){
				$ratio = $width / $size[0];
				$size[0] = $width;
				$size[1] *= $ratio;
			}
			if($size[1] > 80){
				$ratio = 80 / $size[1];
				$size[1] = 80;
				$size[0] *= $ratio;
			}
			$img_size = "width = '$size[0]' height = '$size[1]'";
			$top = ((100-$size[1]) / 2)."px";
			$left = (($width-$size[0]) / 2)."px";
			if($shadow) $out = "<div class='shadow'>";
			$out .= "<img id = 'photo_partenaire_$this->id' style = 'padding: $top $left' src = 'images/partenaires/$this->image' alt = '' $img_size />\n";
			if($shadow) $out .= "</div>\n";
			if($this->url) $out = "<div onclick = \"self.location.href = '$this->url'\" class = 'pointer'>$out</div>";
		}
		else return false;
		return $out;
	}

	function icon(){
		$doctype = (ereg(".*\.([a-zA-Z]*)",$this->fichier,$regs)) ? strtolower($regs[1]) : "txt";
		return "<img src = 'images/icon_$doctype.png' alt = '$doctype' width = '14' height = '14' />\n";

	}

}
?>